package com.xhga.common;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * Created by Hua wb on 2018/11/29.
 */
public class RpcEncoder extends MessageToByteEncoder {
    private Class<?> cls;

    public RpcEncoder(Class<?> cls) {
        this.cls = cls;
    }

    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
        if (cls.isInstance(msg)) {
            byte[] bytes = ProtostuffUtil.serialize(msg);
            out.writeBytes(bytes);
        }
    }
}
